clear
clear matrix
capture log close
set more off
set mem 500m
set matsize 11000


******************* Table 6 permutations ******************

use data_pt_pop_1891, clear

*** Fix manchester and salford
replace loc="manchester" if loc=="salford"
count
collapse (sum) pop, by(loc year)
count

sort loc

merge loc using data_town_cotton_shr_1851

tab _merge
keep if _merge==3
drop _merge


** LHS var
gen log_pop=log(pop)

sort loc year
gen gr_pop=log_pop-log_pop[_n-1] if loc==loc[_n-1]


** RHS var
gen cotton10=0
replace cotton10=1 if shr_cot>.1


gen p1861=0
replace p1861=1 if year==1861
gen p1871=0
replace p1871=1 if year==1871
gen p1881=0
replace p1881=1 if year==1881
gen p1891=0
replace p1891=1 if year==1891

gen p1861cot10=p1861*cotton10
gen p1871cot10=p1871*cotton10
gen p1881cot10=p1881*cotton10
gen p1891cot10=p1891*cotton10


* Limit to textile cities only
gen shr_tex = shr_wool + shr_other_tex
gen tex=0
replace tex=1 if shr_tex>.1
replace tex=1 if shr_cot>.1
keep if tex==1

************ Regressions ***************

*** Panel regregssions
encode loc, gen(loc_code)
xtset loc_code year, yearly
drop if gr_pop==.

xi i.year

save temp_prepared_permutation_data, replace


************ Table 6, Column (2) ***************
use temp_prepared_permutation_data, clear

keep if year>1841 & year<1881

xtreg gr_pop p1871cot10  _I*, fe vce(robust)
matrix alpha=e(b)
matrix beta=alpha[1,1]
matrix drop alpha


forval i=1/10000 {

* Generate randomized treatment variable
preserve
keep loc
duplicates drop
sort loc
gen ordering=runiform()
sort ordering
gen counter=_n
gen treated=0
replace treated=1 if count<11
keep loc treated
sort loc
save temp_random_treatment_indicator, replace
restore

* Merge random treatment into main database
sort loc
merge loc using temp_random_treatment_indicator
tab _merge
drop _merge

* Produce placebo explanatory variables
gen treat1861=p1861*treated
gen treat1871=p1871*treated
gen treat1881=p1881*treated
gen treat1891=p1891*treated

* Run regression and save outpute
xtreg gr_pop treat1871  _I*, fe vce(robust)
matrix alpha=e(b)
matrix beta=beta\alpha[1,1]
matrix drop alpha

drop treat*		
								
} 



matrix treated=beta[1,1]
matrix permuted=beta[2...,1]


clear
svmat treated
svmat permuted

replace treated=treated[_n-1] if treated==.

gen test=0
replace test=1 if permuted<=treated

gen test_count=1
collapse(sum) test test_count
gen reject_pr=test/test_count

gen column=2
save results_table_6_column_2_permutations, replace



************ Table 6, Column (3) ***************
use temp_prepared_permutation_data, clear

keep if year<1881

xtreg gr_pop p1871cot10  _I*, fe vce(robust)
matrix alpha=e(b)
matrix beta=alpha[1,1]
matrix drop alpha


forval i=1/10000 {

* Generate randomized treatment variable
preserve
keep loc
duplicates drop
sort loc
gen ordering=runiform()
sort ordering
gen counter=_n
gen treated=0
replace treated=1 if count<11
keep loc treated
sort loc
save temp_random_treatment_indicator, replace
restore

* Merge random treatment into main database
sort loc
merge loc using temp_random_treatment_indicator
tab _merge
drop _merge

* Produce placebo explanatory variables
gen treat1861=p1861*treated
gen treat1871=p1871*treated
gen treat1881=p1881*treated
gen treat1891=p1891*treated

* Run regression and save outpute
xtreg gr_pop treat1871  _I*, fe vce(robust)
matrix alpha=e(b)
matrix beta=beta\alpha[1,1]
matrix drop alpha

drop treat*		
								
} 



matrix treated=beta[1,1]
matrix permuted=beta[2...,1]


clear
svmat treated
svmat permuted

replace treated=treated[_n-1] if treated==.

gen test=0
replace test=1 if permuted<=treated

gen test_count=1
collapse(sum) test test_count
gen reject_pr=test/test_count

gen column=2
save results_table_6_column_3_permutations, replace



************ Table 6, Column (4) ***************
use temp_prepared_permutation_data, clear

xtreg gr_pop p1871cot10 p1881cot10 p1891cot10 _I*, fe vce(robust)
matrix alpha=e(b)
matrix beta=alpha[1,1]
matrix drop alpha


forval i=1/10000 {

* Generate randomized treatment variable
preserve
keep loc
duplicates drop
sort loc
gen ordering=runiform()
sort ordering
gen counter=_n
gen treated=0
replace treated=1 if count<11
keep loc treated
sort loc
save temp_random_treatment_indicator, replace
restore

* Merge random treatment into main database
sort loc
merge loc using temp_random_treatment_indicator
tab _merge
drop _merge

* Produce placebo explanatory variables
gen treat1861=p1861*treated
gen treat1871=p1871*treated
gen treat1881=p1881*treated
gen treat1891=p1891*treated

* Run regression and save outpute
xtreg gr_pop treat1871 treat1881 treat1891 _I*, fe vce(robust)
matrix alpha=e(b)
matrix beta=beta\alpha[1,1]
matrix drop alpha

drop treat*		
								
} 


matrix treated=beta[1,1]
matrix permuted=beta[2...,1]


clear
svmat treated
svmat permuted

replace treated=treated[_n-1] if treated==.

gen test=0
replace test=1 if permuted<=treated

gen test_count=1
collapse(sum) test test_count
gen reject_pr=test/test_count

gen column=2
save results_table_6_column_4_permutations, replace


*********************** Column 5 ***********************
clear

use data_pt_pop_1901, clear

*** Fix manchester and salford
replace loc="manchester" if loc=="salford"
count
collapse (sum) pop, by(loc year)
count

sort loc

merge loc using data_town_cotton_shr_1851

tab _merge
keep if _merge==3
drop _merge


** LHS var
gen log_pop=log(pop)

sort loc year
gen gr_pop=log_pop-log_pop[_n-1] if loc==loc[_n-1]


** RHS var
gen cotton10=0
replace cotton10=1 if shr_cot>.1

gen p1871=0
replace p1871=1 if year==1871
gen p1881=0
replace p1881=1 if year==1881
gen p1891=0
replace p1891=1 if year==1891
gen p1901=0
replace p1901=1 if year==1901

gen p1871cot10=p1871*cotton10
gen p1881cot10=p1881*cotton10
gen p1891cot10=p1891*cotton10
gen p1901cot10=p1901*cotton10

* Limit to textile cities only
gen shr_tex = shr_wool + shr_other_tex
gen tex=0
replace tex=1 if shr_tex>.1
replace tex=1 if shr_cot>.1
keep if tex==1

************ Regressions ***************

*** Panel regregssions
encode loc, gen(loc_code)
xtset loc_code year, yearly
drop if gr_pop==.

xi i.year

save temp_prepared_permutation_data, replace


************ 
use temp_repared_permutation_data, clear


xtreg gr_pop p1871cot10 p1881cot10 p1891cot10 p1901cot10 _I*, fe vce(robust)
matrix alpha=e(b)
matrix beta=alpha[1,1]
matrix drop alpha


forval i=1/10000 {

* Generate randomized treatment variable
preserve
keep loc
duplicates drop
sort loc
gen ordering=runiform()
sort ordering
gen counter=_n
gen treated=0
replace treated=1 if count<11
keep loc treated
sort loc
save temp_random_treatment_indicator, replace
restore

* Merge random treatment into main database
sort loc
merge loc using temp_random_treatment_indicator
tab _merge
drop _merge

* Produce placebo explanatory variables
gen treat1871=p1871*treated
gen treat1881=p1881*treated
gen treat1891=p1891*treated
gen treat1901=p1901*treated

* Run regression and save outpute
xtreg gr_pop treat1871 treat1881 treat1891 treat1901 _I*, fe vce(robust)
matrix alpha=e(b)
matrix beta=beta\alpha[1,1]
matrix drop alpha

drop treat*		
								
} 



matrix treated=beta[1,1]
matrix permuted=beta[2...,1]


clear
svmat treated
svmat permuted

replace treated=treated[_n-1] if treated==.

gen test=0
replace test=1 if permuted<=treated

gen test_count=1
collapse(sum) test test_count
gen reject_pr=test/test_count

gen column=2
save results_table_6_column_5_permutations, replace



****
